package prj.carmap.mobile;


import java.util.List;
import java.util.Vector;

import prj.carmap.mobile.activity.DialogView;
import prj.carmap.mobile.model.Config;
import prj.carmap.mobile.model.SellerType;
import prj.carmap.mobile.model.UserInfo;
import prj.carmap.mobile.process.HotKeyProcess;
import android.app.Activity;
import android.app.Application;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;

import com.baidu.mapapi.map.LocationData;
import com.miaowang.salad.Common;
import com.path.android.jobqueue.JobManager;
import com.path.android.jobqueue.config.Configuration;
import com.path.android.jobqueue.log.CustomLogger;


public class CarmapApplication extends Application {

	/** 自身实例引用 */
	private static CarmapApplication mInstance = null;
	JobManager jobManager;

	/** Activity集合 */
	public static Vector<Activity> activities = new Vector<Activity>();

	public static String SERVER_URL;

	//调试用
	//public static final String strKey = "dRhyWWcnQahIGl0tGZ0iA6N2";
	//发布用
	/** 百度地图key */
//	public static final String strKey = "pIx1KuvKM8Vm5WgPWq3vWVOj";
//	public static final String strKey = "8zjUfGo4vhddAobePhlyzYrq";
//	public static final String strKey = "WBrzDzxehH7Wnl0vgfqGxGhB";
//	public static final String strKey = "lQgGgSWBTr4vYOT4zk7mVzyo";
	public static final String strKey = "3iXTFz0fcusrzKYP4DAMeWgp";

	/** 当前页面标识，用于底部按钮选中状态等 */
	public static String currentPage;

	/** 汽车服务类型缓存 */
	public static List<SellerType> autoSellers;
	
	/** 生活服务类型缓存 */
	public static List<SellerType> liveSellers;
	
	/** 商家类型缓存 */
	public static List<SellerType> sellerTypes;

	/** 配置信息 */
	public static Config config = new Config();

	/** 对话框 */
	public static DialogView popView;


	//public boolean m_bKeyRight = true;
	/** 用户是否登录标识 */
	public boolean isLogin = false;
	/** 登录用户信息 */
	public UserInfo userInfo;

	/** 定位信息 */
	private LocationData locData = null;

	@Override
	public void onCreate() {
		super.onCreate();

		mInstance = this;

		new HotKeyProcess().getHotKeyConfig();
		Common.gobleContext = this;
		configJobManager();
	}
	
	private void configJobManager() {

		Configuration configuration = new Configuration.Builder(CarmapApplication.this)
				.customLogger(new CustomLogger() {

					@Override
					public boolean isDebugEnabled() {
						// TODO Auto-generated method stub
						return false;
					}

					@Override
					public void e(Throwable arg0, String arg1, Object... arg2) {

					}

					@Override
					public void e(String arg0, Object... arg1) {

					}

					@Override
					public void d(String arg0, Object... arg1) {

					}
				})
				.minConsumerCount(1)// always keep at least one consumer alive
				.maxConsumerCount(3)// up to 3 consumers at a time
				.loadFactor(3)// 3 jobs per consumer
				.consumerKeepAlive(120)// wait 2 minute
				.build();
		
		jobManager = new JobManager(CarmapApplication.this, configuration);
	}
	
	 /**
     * get jobManager
     *
     * @return
     */
    public JobManager getJobManager() {
        return jobManager;
    }

	/**
	 * 自身实例引用
	 */
	public static CarmapApplication getInstance() {
		return mInstance;
	}

	public static void addActivity(Activity activity) {
		for (int i = activities.size() - 1; i >= 0; i--)
		{
			Activity temp = activities.get(i);
			if (temp.isFinishing()) {
				activities.remove(i);
			}
		}

		activities.add(activity);
	}

	public LocationData getLocData() {
		return locData;
	}

	public void setLocData(LocationData locData) {
		this.locData = locData;
	}

	/**
	 * 得到自身安装包信息
	 */
	public PackageInfo getPackageInfo() {
		PackageInfo info = null;
		try {
			info = getPackageManager().getPackageInfo(getPackageName(), 0);
		}
		catch (NameNotFoundException e) {
			e.printStackTrace(System.err);
		}
		if (info == null)
			info = new PackageInfo();
		return info;
	}
}
